package com.kuaiyin.controller.request;


import com.kuaiyin.dto.TestCaseDTO;
import com.kuaiyin.result.Result;
import com.kuaiyin.entity.TestCase;
import com.kuaiyin.service.ITestCaseService;
import com.kuaiyin.utils.PageResult;
import com.kuaiyin.vo.TestCaseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 测试用例表（assertions可选） 前端控制器
 * </p>
 *
 * @author ng
 * @since 2025-04-27
 */
@Slf4j
@Tag(name = "测试用例管理")
@RestController
@RequestMapping("/case")
public class TestCaseController {
    @Autowired
    private ITestCaseService testCaseService;

    @Operation(summary = "新增用例")
    @PostMapping()
    public Result<String> saveTestCase(@Valid @RequestBody TestCaseDTO testCaseDTO) {
        testCaseService.savaTestCase(testCaseDTO);
        return Result.success("新增成功");
    }

    @Operation(summary = "查询用例详情")
    @GetMapping("/{id}")
    public Result<TestCaseVO> getTestCase(@PathVariable Long id) {
        TestCaseVO testCaseVO = testCaseService.getById(id);
        return Result.success(testCaseVO);
    }

    @Operation(summary = "更新用例")
    @PutMapping("/{id}")
    public Result<String> updateTestCase(@PathVariable Long id, @Valid @RequestBody TestCaseDTO testCaseDTO) {
        testCaseService.updateTestCase(id, testCaseDTO);
        return Result.success("更新成功");
    }

    @Operation(summary = "删除用例")
    @DeleteMapping("/{id}")
    public Result<String> deleteTestCase(@PathVariable Long id) {
        testCaseService.removeById(id);
        return Result.success("删除成功");
    }

    @Operation(summary = "更新用例状态")
    @PutMapping("/{id}/status/{status}")
    public Result<String> updateTestCaseStatus(@PathVariable Long id, @PathVariable Integer status) {
        testCaseService.updateTestCaseStatus(id, status);
        return Result.success("更新成功");
    }


    @Operation(summary = "查询用例列表")
    @GetMapping("/all")
    public Result<List<TestCaseVO>> getTestCaseList() {
        List<TestCaseVO> testCaseList = testCaseService.getTestCaseList();
        return Result.success(testCaseList);
    }

    @Operation(summary = "分页查询用例列表")
    @GetMapping("/search")
    public Result<PageResult<TestCaseVO>> getTestCase(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String requestId) {
        PageResult<TestCaseVO> testCasePage = testCaseService.getTestCasePage(page, size,
                wrapper -> {
                    if (StringUtils.hasText(name)) {
                        wrapper.like(TestCase::getName, name);
                    }
                    if (StringUtils.hasText(requestId)) {
                        wrapper.eq(TestCase::getRequestId, requestId);
                    }
                    wrapper.orderByDesc(TestCase::getId); // 按照 id 倒序
                });
        return Result.success(testCasePage);
    }
}

